package service.impl;

import constant.Constant;
import dao.GoldPointsDao;
import entity.GoldPoints;
import exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.GoldPointsService;
import vo.GoldPointsVo;

import java.util.Date;

@Service
public class GoldPointsServiceImpl implements GoldPointsService {

    @Autowired
    GoldPointsDao goldPointsDao;

    //    签到获取积分
    @Override
    public boolean addSignIn(Integer user_id, Date create_date) throws ServiceException {
        if (null == user_id) {
            throw new ServiceException("用户id不能为空！");
        }
//        封装参数
        GoldPoints goldPoints = new GoldPoints(user_id, Constant.Point_Count_5,
                0, Constant.GoldPoints_Info, create_date);

        int result = goldPointsDao.insertGoldPoints(goldPoints);
        return 1 == result ? true : false;
    }

    //    根据用户id查询总积分数和总金币数
    @Override
    public GoldPointsVo findFrontSumByUid(Integer user_id) throws ServiceException {
        return goldPointsDao.selectFrontSumByUid(user_id);
    }

    //    10积分兑换1金币
    @Override
    public boolean conversionGold(Integer user_id, Integer point_count) throws ServiceException {
//        对输入的积分进行处理（10的整数倍）
        if (point_count <= 0) {
            throw new ServiceException("请正确输入积分数值（10的整数倍）");
        }
        point_count = point_count / 10 * 10;

//        查询积分是否充足
        GoldPointsVo goldPointsVo = goldPointsDao.selectFrontSumByUid(user_id);
        if (point_count > goldPointsVo.getSum_point_count()) {
            throw new ServiceException("您的积分不足" + point_count);
        }

//        积分兑换金币(扣减积分，增加金币)
        Integer gold_count = point_count / 10;
        //        封装参数
        GoldPoints goldPoints = new GoldPoints(user_id, -1 * point_count,
                gold_count, Constant.ConversionGold_Info, new Date());

        int result = goldPointsDao.insertGoldPoints(goldPoints);
        return 1 == result ? true : false;
    }

    @Override
    @Transactional
    public void updateGPInfoByUserId(int user_id,int resource_user_id,int cost_number,int cost_type) throws ServiceException {
        String type=cost_type==0?"积分":"金币";
        GoldPoints ugp = new GoldPoints();
        GoldPoints rgp = new GoldPoints();
        Date create_date=new Date();
        ugp.setCreate_date(create_date);
        rgp.setCreate_date(create_date);
        ugp.setUser_id(user_id);
        rgp.setUser_id(user_id);
        if(cost_type== Constant.COST_TYPE_SCORE){
            ugp.setPoint_count(0-cost_number);
            rgp.setPoint_count(cost_number);
        }else {
            ugp.setGold_count(0-cost_number);
            rgp.setGold_count(cost_number);
        }
        ugp.setInfo("查看资源消耗"+cost_number+type);
        rgp.setInfo("获得"+cost_number+type);
        goldPointsDao.insertGPInfo(ugp, cost_type);
        goldPointsDao.insertGPInfo(rgp, cost_type);

    }
}
